home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / gs24src.zip / ZFONT2.C < prev    next >
C/C++ Source or Header  |  1991-12-06  |  7KB  |  233 lines

  1. /* Copyright (C) 1989, 1990, 1991 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* zfont2.c */
  21. /* Font creation operator for Ghostscript */
  22. #include "ghost.h"
  23. #include "errors.h"
  24. #include "oper.h"
  25. #include "gxfixed.h"
  26. #include "gsmatrix.h"
  27. #include "gxdevice.h"
  28. #include "gschar.h"
  29. #include "gxfont.h"
  30. #include "alloc.h"
  31. #include "dict.h"
  32. #include "font.h"
  33. #include "name.h"
  34. #include "packed.h"
  35. #include "store.h"
  36.  
  37. /* Imported procedures */
  38. int add_FID(P2(ref *, gs_font *));    /* in zfont */
  39.  
  40. /* Forward references */
  41. int font_int_param(P6(ref *pdict, ref *pname, int minval, int maxval, int defaultval, int *pvalue));
  42. int build_gs_simple_font(P4(os_ptr, gs_font **, font_type, ref *));
  43. int build_gs_font(P4(os_ptr, gs_font **, font_type, ref *));    /* buildfont0 needs this */
  44.  
  45. /* Global font-related objects */
  46. /* Names of system-known keys in font dictionaries: */
  47. extern ref name_FID;            /* in zfont.c */
  48. extern ref name_FontMatrix;        /* in zfont.c */
  49. private ref name_FontType;
  50. private ref name_WMode;
  51. private ref name_FontBBox;
  52. private ref name_Encoding;
  53. ref name_UniqueID;            /* used by zfont1 */
  54. private ref name_BuildChar;
  55.  
  56. /* The global font directory */
  57. extern gs_font_dir *ifont_dir;
  58.  
  59. /* Initialize the font building operators */
  60. private void
  61. zfont2_init()
  62. {    static names_def fnd2[] = {
  63.  
  64.     /* Create the names of the standard elements of */
  65.     /* a font dictionary. */
  66.        { "FontType", &name_FontType },
  67.        { "WMode", &name_WMode },
  68.        { "FontBBox", &name_FontBBox },
  69.        { "Encoding", &name_Encoding },
  70.        { "UniqueID", &name_UniqueID },
  71.        { "BuildChar", &name_BuildChar },
  72.  
  73.     /* Mark the end of the initalized name list. */
  74.        names_def_end
  75.     };
  76.  
  77.     init_names(fnd2);
  78. }
  79.  
  80. /* .buildfont3 */
  81. /* Build a type 3 (user-defined) font. */
  82. int
  83. zbuildfont3(os_ptr op)
  84. {    int code;
  85.     ref *pbuildchar;
  86.     gs_font *pfont;
  87.     check_type(*op, t_dictionary);
  88.     code = dict_find(op, &name_BuildChar, &pbuildchar);
  89.     if ( code <= 0 ) return e_invalidfont;
  90.     check_proc(*pbuildchar);
  91.     return build_gs_simple_font(op, &pfont, ft_user_defined, pbuildchar);
  92. }
  93.  
  94. /* ------ Initialization procedure ------ */
  95.  
  96. op_def zfont2_op_defs[] = {
  97.     {"1.buildfont3", zbuildfont3},
  98.     op_def_end(zfont2_init)
  99. };
  100.  
  101. /* ------ Subroutines ------ */
  102.  
  103. /* Get an integer parameter from a font-related dictionary. */
  104. /* Return 0 if found, 1 if defaulted, <0 if missing or out of range. */
  105. int
  106. font_int_param(ref *pdict, ref *pname, int minval, int maxval,
  107.   int defaultval, int *pvalue)
  108. {    ref *pdval;
  109.     if ( dict_find(pdict, pname, &pdval) <= 0 )
  110.        {    *pvalue = defaultval;
  111.         return 1;
  112.        }
  113.     if ( !r_has_type(pdval, t_integer) )
  114.         return e_typecheck;
  115.     if ( pdval->value.intval < minval || pdval->value.intval > maxval )
  116.         return e_rangecheck;
  117.     *pvalue = (int)pdval->value.intval;
  118.     return 0;
  119. }        
  120.  
  121. /* Do the common work for building a font of any non-composite FontType. */
  122. /* The caller guarantees that *op is a dictionary. */
  123. int
  124. build_gs_simple_font(os_ptr op, gs_font **ppfont, font_type ftype, ref *pbuildchar)
  125. {    ref *pbbox;
  126.     float bbox[4];
  127.     long unique_id;
  128.     ref *puniqueid;
  129.     int code;
  130.     gs_font *pfont;
  131.     if ( dict_find(op, &name_FontBBox, &pbbox) <= 0 )
  132.       return e_invalidfont;
  133.     switch ( r_type(pbbox) )
  134.        {
  135.     default: return e_invalidfont;
  136.     case t_array: case t_mixedarray: case t_shortarray: ;
  137.         if ( r_size(pbbox) != 4 ) return e_invalidfont;
  138.        }
  139.        {    ushort *pbe = pbbox->value.packed;
  140.         ref rbe[4];
  141.         int i;
  142.         for ( i = 0; i < 4; i++ )
  143.            {    packed_get(pbe, rbe + i);
  144.             pbe = packed_next(pbe);
  145.            }
  146.         if ( num_params(rbe + 3, 4, bbox) < 0 )
  147.             return e_invalidfont;
  148.        }
  149.     /* If no UniqueID entry, set the UniqueID member to -1, */
  150.     /* because UniqueID need not be present in all fonts, */
  151.     /* and if it is, the legal range is 0 to 2^24-1. */
  152.     if ( dict_find(op, &name_UniqueID, &puniqueid) <= 0 )
  153.         unique_id = -1;
  154.     else
  155.        {    if ( !r_has_type(puniqueid, t_integer) ||
  156.              puniqueid->value.intval < 0 ||
  157.              puniqueid->value.intval > ((1L << 24) - 1)
  158.            )
  159.             return e_invalidfont;
  160.         unique_id = puniqueid->value.intval;
  161.        }
  162.     code = build_gs_font(op, ppfont, ftype, pbuildchar);
  163.     if ( code != 0 ) return code;    /* invalid or scaled font */
  164.     pfont = *ppfont;
  165.     pfont->data.base.FontBBox.p.x = bbox[0];
  166.     pfont->data.base.FontBBox.p.y = bbox[1];
  167.     pfont->data.base.FontBBox.q.x = bbox[2];
  168.     pfont->data.base.FontBBox.q.y = bbox[3];
  169.     pfont->data.base.UniqueID = unique_id;
  170.     return 0;
  171. }
  172.  
  173. /* Do the common work for building a font of any FontType. */
  174. /* The caller guarantees that *op is a dictionary. */
  175. /* Return 0 for a new font, 1 for a font made by makefont or scalefont, */
  176. /* or a negative error code. */
  177. int
  178. build_gs_font(os_ptr op, gs_font **ppfont, font_type ftype, ref *pbuildchar)
  179. {    ref *pftype;
  180.     ref *pmatrix;
  181.     gs_matrix mat;
  182.     ref *pencoding;
  183.     int wmode;
  184.     int code;
  185.     gs_font *pfont;
  186.     ref *pfid;
  187.     ref *aop = dict_access_ref(op);
  188.     if ( dict_find(op, &name_FontType, &pftype) <= 0 ||
  189.         !r_has_type(pftype, t_integer) ||
  190.         pftype->value.intval != (int)ftype ||
  191.         dict_find(op, &name_FontMatrix, &pmatrix) <= 0 ||
  192.         dict_find(op, &name_Encoding, &pencoding) <= 0 ||
  193.         read_matrix(pmatrix, &mat) < 0
  194.        )
  195.       return e_invalidfont;
  196.     switch ( r_type(pencoding) )
  197.        {
  198.     default: return e_invalidfont;
  199.     case t_array: case t_mixedarray: case t_shortarray: ;
  200.        }
  201.     code = font_int_param(op, &name_WMode, 0, 1, 0, &wmode);
  202.     if ( code < 0 ) return code;
  203.     code = dict_find(op, &name_FID, &pfid);
  204.     if ( r_has_attr(aop, a_write) )
  205.        {    /* Assume this is a new font */
  206.         font_data *pdata;
  207.         if ( code > 0 ) return e_invalidfont;    /* has FID already */
  208.         if ( (pfont = (gs_font *)alloc(1, sizeof(gs_font), "buildfont(font)")) == 0 ||
  209.              (pdata = (font_data *)alloc(1, sizeof(font_data), "buildfont(data)")) == 0
  210.            )
  211.           return e_VMerror;
  212.         if ( (code = add_FID(op, pfont)) < 0 ) return code;
  213.         ref_assign(&pdata->dict, op);
  214.         ref_assign(&pdata->BuildChar, pbuildchar);
  215.         ref_assign(&pdata->Encoding, pencoding);
  216.         pfont->base = pfont;
  217.         pfont->dir = ifont_dir;
  218.         pfont->client_data = (char *)pdata;
  219.         pfont->FontType = ftype;
  220.         pfont->FontMatrix = mat;
  221.         pfont->WMode = wmode;
  222.         pfont->build_char_proc = gs_no_build_char_proc;
  223.        }
  224.     else
  225.        {    /* Assume this was made by makefont or scalefont */
  226.         if ( code <= 0 || !r_has_type(pfid, t_fontID) )
  227.             return e_invalidfont;
  228.         pfont = pfid->value.pfont;
  229.        }
  230.     *ppfont = pfont;
  231.     return 0;
  232. }
  233.